package test.domain.environment;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Properties;

import test.client.support.thread.DefaultThreadManager.RunMode;
import test.client.support.thread.DefaultThreadManager.SleepMode;

public class MetaThread implements Environment<MetaThread>, Externalizable {
	private int maxThreadCount;
	
	private SleepMode sleepMode;
	private RunMode runMode;
	
	private int loopSleepTime;
	private int loopMaxThreadCount;
	private int threadSleepTime;
	private int threadStartId;
	
	@Override
	public MetaThread initialize(Properties prop) {
		this.maxThreadCount = Integer.parseInt(prop.getProperty("openfire.test.thread.max", "10"));
		
		this.sleepMode = SleepMode.valueOf(prop.getProperty("openfire.test.mode.sleep", "thread"));
		this.runMode = RunMode.valueOf(prop.getProperty("openfire.test.mode.run", "ordered"));
		
		this.loopSleepTime = Integer.parseInt(prop.getProperty("openfire.test.loop.sleeptime", "5000"));
		this.loopMaxThreadCount = Integer.parseInt(prop.getProperty("openfire.test.loop.maxthreads", "100"));
		this.threadSleepTime = Integer.parseInt(prop.getProperty("openfire.test.thread.sleeptime", "50"));
		this.threadStartId = Integer.parseInt(prop.getProperty("openfire.test.thread.startid", "0"));
				
		return this;
	}
	
	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeInt(this.maxThreadCount);
		out.writeObject(sleepMode.toString());
		out.writeObject(runMode.toString());
		out.writeInt(this.loopSleepTime);
		out.writeInt(this.loopMaxThreadCount);
		out.writeInt(this.threadSleepTime);
		out.writeInt(this.threadStartId);
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		this.maxThreadCount = in.readInt();
		this.sleepMode = SleepMode.valueOf((String) in.readObject());
		this.runMode = RunMode.valueOf((String) in.readObject());
		this.loopSleepTime = in.readInt();
		this.loopMaxThreadCount = in.readInt();
		this.threadSleepTime = in.readInt();
		this.threadStartId = in.readInt();
	}

	public int getMaxThreadCount() {
		return maxThreadCount;
	}
	public void setMaxThreadCount(int maxThreadCount) {
		this.maxThreadCount = maxThreadCount;
	}
	public SleepMode getSleepMode() {
		return sleepMode;
	}
	public void setSleepMode(SleepMode sleepMode) {
		this.sleepMode = sleepMode;
	}
	public RunMode getRunMode() {
		return runMode;
	}
	public void setRunMode(RunMode runMode) {
		this.runMode = runMode;
	}
	public int getLoopSleepTime() {
		return loopSleepTime;
	}
	public void setLoopSleepTime(int loopSleepTime) {
		this.loopSleepTime = loopSleepTime;
	}
	public int getLoopMaxThreadCount() {
		return loopMaxThreadCount;
	}
	public void setLoopMaxThreadCount(int loopMaxThreadCount) {
		this.loopMaxThreadCount = loopMaxThreadCount;
	}
	public int getThreadSleepTime() {
		return threadSleepTime;
	}
	public void setThreadSleepTime(int threadSleepTime) {
		this.threadSleepTime = threadSleepTime;
	}
	public int getThreadStartId() {
		return threadStartId;
	}
	public void setThreadStartId(int threadStartId) {
		this.threadStartId = threadStartId;
	}
}
